<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/03/13 19:55:39 $)
// $RCSfile: visitor_updatefootprint.php,v $ : $Revision: 1.9 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

headerNoCache();

$template->loadLanguageSection("livesupportclient");

// ======= Load Up the functions file =======
require_once ("./modules/livesupport/functions_livesupport.php");

// Try to get the session
if (!$session->fetchSession($_GET["sessionid"], SESSION_VISITOR))
{
	$isbanned = checkBannedStatus(true);
	if (!$isbanned)
	{
		$session->insertSession(SESSION_VISITOR, 0, $_GET["sessionid"], true);
		$_GET["isfirsttime"] = 1;
	}
}

// Update this session last activity
if (!empty($_SWIFT["session"]["sessionid"]))
{
	$dbCore->shutdownQuery("UPDATE `". TABLE_PREFIX ."sessions` SET `lastactivity` = '". DATENOW ."' WHERE `sessionid` = '". $dbCore->escape($_SWIFT["session"]["sessionid"]) ."';");
}


/**
* Is the page being loaded for first time?
*/
if ($_GET["isfirsttime"] == 1) {
	/**
	* Calculate stuff and insert footprint
	*/
	$properties = array();
	$properties["browsername"] = $_GET["browsername"];
	$properties["browserversion"] = $_GET["browserversion"];
	$properties["appversion"] = $_GET["appversion"];
	$properties["browsercode"] = $_GET["browsercode"];
	$properties["operatingsys"] = $_GET["operatingsys"];
	$properties["platform"] = $_GET["platform"];
	$properties["resolution"] = $_GET["resolution"];
	$properties["colordepth"] = $_GET["colordepth"];
	$properties["pagetitle"] = $_GET["pagetitle"];
	$properties["location"] = $_GET["url"];
	$properties["hasnotes"] = $_GET["hasnotes"];
	$properties["sessionid"] = $_GET["sessionid"];
	$properties["ipaddress"] = $_SERVER["REMOTE_ADDR"];
	$properties["hostname"] = gethostbyaddr($_SERVER["REMOTE_ADDR"]);

	$properties["countrycode"] = $_GET["countrycode"];
	$properties["country"] = $_GET["country"];

	$properties["campaignid"] = $_GET["campaignid"];
	$properties["campaigntitle"] = $_GET["campaigntitle"];

	$search = checkSearchEngine($_GET["referrer"]);
	if (is_array($search)) {
		// User came from some search engine.
		$properties["searchenginename"] = $search["name"];
		$properties["searchstring"] = $search["query"];
		$properties["searchengineurl"] = $search["url"];
		$properties["rowbgcolor"] = $_SWIFT["settings"]["livesupport_searchbgcolor"];
		$properties["rowfrcolor"] = $_SWIFT["settings"]["livesupport_searchfrcolor"];
	}

	if (!empty($_GET["campaignid"])) // Update the Ad Tracking Color if this visitor has a campaign set
	{
		$properties["rowbgcolor"] = $_SWIFT["settings"]["ls_adbgcolor"];
		$properties["rowfrcolor"] = $_SWIFT["settings"]["ls_adfrcolor"];
	}

	$properties["referrer"] = $_GET["referrer"];

	insertVisitorFootprint($properties);

} else if ($_GET["isfirsttime"] == 0) {
	if (buildRand(1, 10) == 10)
	{
		if (defined("USESHUTDOWNFUNC"))
		{
			register_shutdown_function("flushVisitors");
		} else {
			flushVisitors();
		}
	}

	if (!empty($_SWIFT["session"]["sessionid"]))
	{
		updateVisitorFootprint($_SWIFT["session"]["sessionid"], $_GET["url"]);
	}
}

if ($_SWIFT["session"]["status"] == PROACTIVE_FORCED)
{
	echoImage("2");
	resetVisitorStatus($_SWIFT["session"]["sessionid"]);
} else if ($_SWIFT["session"]["status"] == PROACTIVE_REQUEST) {
	echoImage("3");
	resetVisitorStatus($_SWIFT["session"]["sessionid"]);
} else {
	echoImage("1");
}

exit;
?>